package com.qqtech.qquan.share.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.agent.context.AdminPage;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.share.enums.ShareConst;
import com.qqtech.qquan.share.model.ShareSysParam;
import com.qqtech.qquan.share.model.ShareSysParamQuery;
import com.qqtech.qquan.share.model.ShareSysParamVo;
import com.qqtech.qquan.share.service.ShareSysParamService;
import com.qqtech.qquan.sys.controller.BaseAgentController;

@Controller
@RequestMapping(value = "/share/sys/param")
public class ShareSysParamController extends BaseAgentController {

	@Resource
	private ShareSysParamService shareSysParamService;
	
	@Override
	protected BaseService<ShareSysParam> getBaseService() {
		return shareSysParamService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/share/shareSysParam-list";
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(ShareSysParamQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<ShareSysParamVo> page = shareSysParamService.queryPageList(query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(page.getNumberOfElements());
		return adminPage;
	}
	
	/**
	 * 去新增
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/toAdd")
	public String toAdd(Model model) {
		return "/share/shareSysParam-add";
	}
	
	/**
	 * 创建成功
	 * 
	 * @param add
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add(ShareSysParam shareSysParam) {
		Result ret = new Result();
		shareSysParamService.insert(shareSysParam);
		return ret;
	}
	
	/**
	 * 去更新
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/update")
	public String update(Integer id, Model model) {
		model.addAttribute("start_time", ShareConst.PARAMKEY_SHARE_ORDER_AWARD_START_TIME);
		model.addAttribute("end_time", ShareConst.PARAMKEY_SHARE_ORDER_AWARD_END_TIME);
		ShareSysParamVo shareSysParamVo = shareSysParamService.getById(id);
		model.addAttribute("shareSysParamVo", shareSysParamVo);
		return "/share/shareSysParam-update";
	}
	
	/**
	 * 创建成功
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/doUpdate")
	@ResponseBody
	public Result doUpdate(ShareSysParam shareSysParam) {
		Result ret = new Result();
		shareSysParamService.updateById(shareSysParam);
		shareSysParamService.clearAll();
		return ret;
	}
}
